package com.probiz.estore.customer.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.customer.Consult;
import com.probiz.estore.common.util.SearchUtil;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.customer.dao.ConsultDao;
import com.probiz.estore.customer.model.ConsultSearchCriteria;

/**
 * Dao implementation for Consult.
*/
public class ConsultDaoImpl extends HibernateGenericDaoImpl<Consult> implements ConsultDao {
	
	protected void initFilterHqlMap() {
		filterHqlMap.put(Consult.FilterHqlName.MY_CONSULT_LIST, "from Consult c where c.customer.appuserId=? and c.parent is null order by c.createTime desc");
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.ConsultDao#getCountsOfCustomer(java.lang.Integer)
	 */
	public Integer[] getCountsOfCustomer(Integer customerId){
		int myConsultListCount = this.countByFilter(Consult.FilterHqlName.MY_CONSULT_LIST, customerId).intValue();
		Integer[] counts = new Integer[1];
		counts[0] = Integer.valueOf(myConsultListCount);
		return counts;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.ConsultDao#searchByCriteria(com.probiz.estore.customer.model.ConsultSearchCriteria, com.probiz.estore.core.search.SearchCriteria)
	 */
	public List<Consult> searchByCriteria(ConsultSearchCriteria consultSearchCriteria, SearchCriteria searchCriteria){
		StringBuffer hqlsb = new StringBuffer(100);
	       List<Object> paramList = new ArrayList<Object>();
	       //start:构建HQL语句
	       hqlsb.append("select c from Consult c left join c.customer as u where 1=1 ");
	      
	       //-begin:*********回复的咨询id，该属性非常重要*********
	       if (null != consultSearchCriteria.getParentId()) {
	    	   hqlsb.append(" and  c.parent.consultId = ? ");
	    	   paramList.add(consultSearchCriteria.getParentId());
	       }else{
	    	   hqlsb.append(" and  c.parent is null ");
	       }
	       //-end:
	       //-begin:标题
	       if (StringUtils.isNotBlank(consultSearchCriteria.getSubject())) {
	           hqlsb.append(" and  c.subject like ? ");
	           paramList.add("%" + consultSearchCriteria.getSubject() + "%");
	       }
	       //-end:标题
	       //-begin:内容
	       if (StringUtils.isNotBlank(consultSearchCriteria.getMessage())) {
	    	   hqlsb.append(" and  c.message like ? ");
	    	   paramList.add("%" + consultSearchCriteria.getMessage() + "%");
	       }
	       //-end:内容
	       
	     //-begin:关键词
	       if (StringUtils.isNotBlank(consultSearchCriteria.getKeywords())) {
	           String keywords = SearchUtil.getSqlQueryValue(consultSearchCriteria.getKeywords());
	           hqlsb.append(" and (");
	           hqlsb.append("    c.product.productName like ? ");
	           hqlsb.append(" or c.product.keywords like ? ");
	           hqlsb.append(" or c.product.productCode like ? ");
	           hqlsb.append(" or c.product.category.categoryName like ? ");
	           hqlsb.append(" or c.product.category.keywords like ? ");
	           hqlsb.append(" or c.message like ? ");
	           hqlsb.append(" )");
	           for (int i = 0; i < 6; i++) {
	               paramList.add("%" + keywords + "%");
	           }
	       }
	       //-end:关键词
	     //-begin:商品id
	       if (null != consultSearchCriteria.getProductId()) {
	    	   hqlsb.append(" and  c.product.productId = ? ");
	    	   paramList.add( consultSearchCriteria.getProductId());
	       }
	       //-end:商品id
	       //-start:商品编码
	       if (StringUtils.isNotBlank(consultSearchCriteria.getProductCode())) {
	           hqlsb.append(" and c.product.productCode = ?");
	           paramList.add(consultSearchCriteria.getProductCode());
	       }
	       //-end:商品编码
	       //-start:顾客名称
	       if (StringUtils.isNotBlank(consultSearchCriteria.getConsultUserName())) {
	    	   hqlsb.append(" and ( (c.customerName like ?)");
	    	   hqlsb.append(" or (u is not null and u.firstname like ?)");
	    	   hqlsb.append(" or (u is not null and u.username like ?) )");
	    	   for (int i = 0; i < 3; i++) {
	    		   paramList.add("%" + consultSearchCriteria.getConsultUserName() + "%");
	    	   }	
	       }
	       //-end:顾客名称
	       //-start:状态:显示/隐藏
	       if (null != consultSearchCriteria.getStatus()) {
	           hqlsb.append(" and c.status = ?");
	           paramList.add(consultSearchCriteria.getStatus());
	       }
	       //-end:状态:显示/隐藏
	       //-start:状态:已读/未读
	       if (null != consultSearchCriteria.getIsRead()) {
	    	   hqlsb.append(" and c.isRead = ?");
	    	   paramList.add(consultSearchCriteria.getIsRead());
	       }
	       //-end:状态:已读/未读
	       //-start:状态:置顶/非置顶
	       if (null != consultSearchCriteria.getSortOrder()) {
	    	   hqlsb.append(" and c.sortOrder = ?");
	    	   paramList.add(consultSearchCriteria.getSortOrder());
	       }
	       //-end:状态:置顶/非置顶
	     //-start:状态:处理状态
	       if (null != consultSearchCriteria.getHandleStatus()) {
	    	   if(consultSearchCriteria.getHandleStatus().equals(Constants.FLAG_TRUE)){
	    		   hqlsb.append(" and c.onShowReplyCount > 0");
	    	   }else{
	    		   hqlsb.append(" and c.onShowReplyCount <= 0");
	    	   }
	       }
	       //-end:状态:处理状态
	       
	       //start:order by
	       if (null != consultSearchCriteria.getOrderby()) {
	           hqlsb.append(" order by ").append(getSortByString(consultSearchCriteria.getOrderby()));
	       }else{
	           hqlsb.append(" order by c.createTime desc,c.consultId desc");
	       }
	       //end:order by
	       
	       //end:构建HQL语句
	       searchCriteria.setHql(hqlsb.toString());
	       searchCriteria.removeParamValues();
	       for (Object object : paramList) {
	           searchCriteria.addParamValue(object);
	       }
	       List<Consult> consultList =  searchByCriteria(searchCriteria);
	       return consultList;
	}
	
	 /**
	    * 根据orderby获得 hql语句
	    * @param orderby 排序id
	    * @return hql语句
	    */
	   private String getSortByString(short orderby) {
	       String orderByString;
	       switch (orderby) {
	           case 3:
	               orderByString = "convert_gbk(c.message) asc";
	               break;
	           case 4:
	               orderByString = "convert_gbk(c.message) desc";
	               break;
	           case 5:
	        	   orderByString = "convert_gbk(c.product.productName) asc";
	        	   break;
	           case 6:
	        	   orderByString = "convert_gbk(c.product.productName) desc";
	        	   break;
	           case 9:
	        	   orderByString = "c.createTime asc";
	        	   break;
	           case 10:
	        	   orderByString = "c.createTime desc";
	        	   break;
	           default:
	               orderByString = "c.createTime desc,c.consultId desc";
	               break;
	       }
	       return orderByString;
	   }
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.ConsultDao#getReplyCountByConsultId(java.lang.Integer)
	 */
	public Long getReplyCountByConsultId(Integer consultId){
		return this.countByHql("from Consult c where c.parent.consultId = ?", consultId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.ConsultDao#getOnShowReplyCountByConsultId(java.lang.Integer)
	 */
	public Long getOnShowReplyCountByConsultId(Integer consultId){
		 return this.countByHql("from Consult c where c.parent.consultId = ? and c.status=1", consultId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.ConsultDao#getConsultCountByProductId(java.lang.Integer)
	 */
	public Long getConsultCountByProductId(Integer productId){
		return this.countByHql("from Consult c where c.product.productId = ? and c.parent is null", productId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.ConsultDao#getOnShowConsultCountByProductId(java.lang.Integer)
	 */
	public Long getOnShowConsultCountByProductId(Integer productId){
		return this.countByHql("from Consult c where c.product.productId = ? and  c.parent is null and c.status=1", productId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.ConsultDao#getOnShowReplyListByConsultId(java.lang.Integer)
	 */
	 
	public List<Consult> getOnShowReplyListByConsultId(Integer consultId){
		return this.findByHql("from Consult c where c.parent.consultId =? and c.status = 1 order by c.createTime asc", consultId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.ConsultDao#countConsultTotal()
	 */
	public Long countConsultTotal(){
		return this.countByHql("from Consult c where c.parent is null ");
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.ConsultDao#countUnreplyConsultTotal()
	 */
	public Long countUnreplyConsultTotal(){
		return this.countByHql("from Consult c where c.parent is null and c.onShowReplyCount <1");
	}
}
